// const Api = require('../api/index.js')
const Config = require('@/config/index.js');

const Util = require('@/utils/sys-util.js');

const accountTips = (tips, code) => {
    uni.showModal({
        title: '账号提示',
        content: tips,
        showCancel: false,
        confirmText: '确认',
        confirmColor: '#FF0000',

        success(res) {
            if (res.confirm) {
                if (code == 2011) {
                    uni.navigateTo({
                        url: '/pages/login/index'
                    });
                }
            }
        }
    });
};

const http = (requestObj) => {
    return new Promise((resolve, reject) => {
        if (requestObj.loading) {
            uni.showLoading({
                title: '请求中..',
                mask: true
            });
        }

        uni.request({
            method: requestObj.method,
            url: getUrl(requestObj.url),
            data: requestObj.param,
            header: {
                'Content-type': 'application/json',
                'Client-Type': Config.clientType,
                Authorization: requestObj.token ? uni.getStorageSync('access_token') : ''
            },
            success: function (res) {
                if (typeof res.data == 'string') {
                    let temp = res.data;
                    let data = Util.decrypted(temp, Config.CryptoJSKey, Config.CryptoJSIv);
                    res.data = JSON.parse(data);
                }

                if (res.statusCode >= 200 && res.statusCode < 300) {
                    let code = res.data.code;

                    if (code != 1) {
                        if (code == 2011) {
                            accountTips('未登录', code);
                        } else if (code == 2008) {
                            accountTips('当前账号已被禁用，若有疑问请联系客服');
                        } else {
                            if (requestObj.showerr) {
                                setTimeout(function () {
                                    uni.showToast({
                                        title: res.data.msg,
                                        icon: 'none'
                                    });
                                }, 200);
                            }

                            resolve(res.data);
                        }
                    } else {
                        resolve(res.data);
                    }
                } else {
                    reject(res);
                }
            },
            fail: function (e) {
                reject(e);
            },
            complete: function (e) {
                if (requestObj.loading) {
                    uni.hideLoading();
                }
            }
        });
    });
};
/**
 * 拼接url
 */

const getUrl = (url) => {
    if (url.indexOf('://') == -1) {
        url = Config.apiUrl + '/' + url;
    }

    return url;
};
/**
 * get方法
 */

const _get = (url, param = {}, token = false, loading = true, showerr = true) => {
    return http({
        url,
        param,
        token,
        loading,
        showerr,
        method: 'get'
    });
};
/**
 * post方法
 */

const _post = (url, param = {}, token = false, loading = true, showerr = true) => {
    return http({
        url,
        param,
        token,
        loading,
        showerr,
        method: 'post'
    });
};
/**
 * 上传图片
 */

const _upload = (imgpath, folder) => {
    return new Promise((resolve, reject) => {
        uni.showLoading({
            title: '上传中..',
            mask: true
        });
        uni.uploadFile({
            url: Config.apiUrl + '/api/file/upload',
            filePath: imgpath,
            name: 'files',
            header: {
                'content-type': 'multipart/form-data',
                'Client-Type': Config.clientType,
                Authorization: uni.getStorageSync('access_token')
            },
            formData: {
                folder: folder
            },

            success(res) {
                if (res.statusCode == 200) {
                    let ret = JSON.parse(res.data);

                    if (ret.code == 1) {
                        resolve(ret.data[0].url);
                    } else {
                        uni.showToast({
                            title: '上传失败',
                            icon: 'none'
                        });
                    }
                }
            },

            fail(err) {
                console.log(err);
            },

            complete() {
                uni.hideLoading();
            }
        });
    });
};

module.exports = {
    _get,
    _post,
    _upload
};
